<template>
  <a-modal 
    :title="title" 
    :width="800" 
    :visible="visible" 
    :confirm-loading="confirmLoading" 
    cancel-text="关闭" 
    @ok="submit" 
    @cancel="closeModal"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="电话">
          <a-input v-decorator="['mobile', validatorRules.mobile ]" placeholder="请输入电话" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="姓名">
          <a-input v-decorator="['name', validatorRules.name ]" placeholder="请输入姓名" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="支付宝账号">
          <a-input v-decorator="['aliAccount', validatorRules.aliAccount ]" placeholder="请输入支付宝账号" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="身份证正面">
          <image-upload v-model="identFront" text="上传" :isMultiple="false"></image-upload>
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="身份证反面">
          <image-upload v-model="identBack" text="上传" :isMultiple="false"></image-upload>
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="邀请人手机">
          <a-input v-decorator="['fromMobile', validatorRules.fromMobile ]" placeholder="请输入邀请人手机" />
        </a-form-item>
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="备注">
          <a-textarea v-decorator="['remark', {} ]" placeholder="请输入备注" :rows="2" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import ApiTree from '@/api/diary/diaryList';
import ImageUpload from '@/components/ImageUpload';
import { Slime } from '@/mixins/slime';
export default {
  name: 'diaryListModal',
  components: { ImageUpload },
  data() {
    return {
      title: '编辑',
      form: this.$form.createForm(this),
      model: {},
      validatorRules: {
        mobile: { rules: [{ required: true, message: '请输入电话!' }] },
        name: { rules: [{ required: true, message: '请输入姓名!' }] },
        aliAccount: {
          rules: [{ required: true, message: '请输入支付宝账号!' }],
        },
        fromMobile: {
          rules: [{ required: true, message: '请输入邀请人手机!' }],
        },
      },
      id: 0,
      identFront: '',
      identBack: '',
      remark: '',
    };
  },
  mixins: [Slime],
  methods: {
    // 增
    crudCreate() {
      this.crudUpdate({});
    },
    // 改
    crudUpdate(record) {
      this.form.resetFields(); //重新设置领域
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.id = record.id;
        this.form.setFieldsValue({
          mobile: record.mobile,
          name: record.name,
          aliAccount: record.aliAccount,
          fromMobile: record.fromMobile,
        });
        this.identFront = record.identFront;
        this.identBack = record.identBack;
        this.remark = record.remark;
      });
    },
    // 提交
    submit() {
      const that = this;
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let param = { 
            ...values,
            identFront: that.identFront,
            identBack: that.identBack,
            remark: that.remark,
          };
          if (!that.id) {
            that.submitCreate(param);
          } else {
            param = {...param, id:that.id}
            that.submitUpdate(param);
          }
        }
      });
    },
    // 提交 增
    submitCreate(param) {
      const that = this;
      ApiTree.getAdd(param)
        .then((res) => {
          that.$message.success('新增成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 提交 改
    submitUpdate(param) {
      const that = this;
      ApiTree.getUpdate(param)
        .then((res) => {
          that.$message.success('编辑成功');
        })
        .finally(() => {
          that.closeModal();
        });
    },
    // 弹窗-关闭
    closeModal() {
      this.confirmLoading = false;
      this.visible = false;
      this.form.resetFields();
      this.$emit('closeModal');
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>